//
//  BLRoomViewController.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/6/1.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit

class BLRoomViewController: UIViewController {
    
    var navBarSuperView : UIView?
    
    
    
    var roomID : String = ""
    var playControlView : UIView?
    var buttonsView : UIView = UIView()


    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navBarSuperView = navigationController?.navigationBar.superview

        view.backgroundColor = UIColor.grayColor()
        setupPlayControlView()
        setButtonsView()
        setupDanmuView()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.removeFromSuperview()
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        guard navigationController?.navigationBar != nil else{
            return
        }
        
        navBarSuperView?.addSubview((navigationController?.navigationBar)!)
    }
    
}


// MARK: - 设置
extension BLRoomViewController{
    
    func setupPlayControlView() {
        
        let playControlVC = BLPlayControlViewController()
        playControlVC.roomID = roomID
        self.addChildViewController(playControlVC)
        view.addSubview(playControlVC.view)
        
        playControlView = playControlVC.view
        
        playControlView!.backgroundColor = UIColor.purpleColor()
        view.addSubview(playControlView!)
        playControlView!.snp_makeConstraints { (make) in
            make.top.equalToSuperview().offset(BL_StatusBarHeight)
            make.left.right.equalToSuperview()
            make.height.equalTo(view.bounds.width / BLVideoRtatio_16x9)
        }
    }
    

    
    /// 弹幕View
    func setupDanmuView() {
        let danmuVC = BLDouyuDanmuTableViewController()
        danmuVC.roomID = roomID
        self.addChildViewController(danmuVC)
        view.addSubview(danmuVC.view)
        danmuVC.view.snp_makeConstraints { (make) in
            make.right.left.bottom.equalToSuperview()
            make.top.equalTo(buttonsView.snp_bottom)
        }
        
    }
}

// MARK: - 中部按钮组
extension BLRoomViewController{
    // 中部按钮组
    func setButtonsView() {
        buttonsView = UIView()
        buttonsView.backgroundColor = bl_randomColor()
        self.view.addSubview(buttonsView)
        
        let danmuButton = setButtons("弹幕")
        let detailButton = setButtons("主播详情")
        
        buttonsView.addSubview(danmuButton)
        buttonsView.addSubview(detailButton)
        
        
        buttonsView.snp_makeConstraints { (make) in
            make.right.left.equalToSuperview()
            make.top.equalTo((playControlView?.snp_bottom)!)
            make.height.equalTo(44)
        }
        
        danmuButton.snp_makeConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        detailButton.snp_makeConstraints { (make) in
            make.top.left.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
    }
    
    private func setButtons(title : String) -> UIButton {
        let danmuButton = UIButton(type: .Custom)
        danmuButton.setTitle(title, forState: UIControlState.Normal)
        danmuButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        danmuButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Selected)
        return danmuButton
        
    }
}